# encoding: utf-8
# @File  : test10_ht_del_product.py
# @Author: wu shaofan
# @Date  :  2024/04/30
# @Desc :
import json
import allure
from faker import Faker
import config
import conftest
from PO.page.page_ht_ProductList import PagehtProductList
from PO.page.page_ht_login import PageHtLogin
from Utils.get_driver import GetDriver
from Utils.record_log import Log


@allure.feature('5、后台商品管理')
class TestAddproduct:
    def setup_class(self):
        # 获取driver
        self.driver = GetDriver().get_driver(config.ht_login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取商品管理对象
        self.product_manage = PagehtProductList(self.driver)
        # 获取后台登录对象
        self.login = PageHtLogin(self.driver)
        # 创建一个Faker对象
        self.fake = Faker(locale='zh_CN')
        # 获取日志对象
        self.log = Log("商品下订单结算用例").get_logger()

    def teardown_class(self):
        # TODO 关闭driver
        GetDriver().close_driver()
        Log("商品下订单结算用例").write_empty_line()

    @allure.story('2、删除商品')
    def test_del_product(self):
        with allure.step('前置条件：登录测试账号'):
            self.login.page_ht_login()

        with allure.step('步骤一：点击商品管理tab菜单'):
            self.product_manage.page_ht_click_shop_tab()

        with allure.step('步骤二：进入iframe框架'):
            self.product_manage.page_ht_enter_iframe()

        with allure.step('步骤二：搜索框输入商品名称'):
            self.product_manage.page_ht_search_product_name(config.add_product_name)

        with allure.step('步骤三：点击搜索按钮'):
            self.product_manage.page_ht_click_search_button()
            self.product_manage.base_time_sleep(0.5)
            # 获取商品勾选框个数
            count = self.product_manage.page_ht_get_product_select_count()
            # 获取删除的商品名称
            name = self.product_manage.page_ht_get_product_name_list()

            if count > 0:
                with allure.step('步骤四：点击全选按钮'):
                    self.product_manage.page_ht_click_select_all_button()
                with allure.step('步骤五：点击批量删除按钮'):
                    self.product_manage.page_ht_click_batch_delete_button()
                with allure.step('步骤六：点击确定删除按钮'):
                    self.product_manage.page_ht_click_confirm_delete_button()
                with allure.step('步骤七：获取删除成功的toast信息'):
                    self.product_manage.base_time_sleep(0.1)
                    info = self.product_manage.page_ht_get_toast_info()
                    self.product_manage.base_get_allure_image()
                    assert info == "操作成功"
            else:
                print("没有商品，无法删除")
                self.log.info("没有商品，无法删除")
                pass

        data = {
            "搜索的商品": config.add_product_name,
            "删除信息": {
                "勾选框个数": count,
                "商品名称列表": name,
                }
            }
        # print(data)
        self.log.info(f"后台删除数据：{data}")
        data = json.dumps(data, ensure_ascii=False, indent=4)
        allure.attach(data, "后台删除数据", allure.attachment_type.JSON)
